<?php

namespace App\Http\Controllers\Admin;

use App\Repository\Zhuandan\GoodsZRepository as model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;
use App\Repository\UploadRepository;

// 分销商
class GoodsZController extends BaseController
{
  public function __construct(model $model)
  {
    $this->repoist = $model;
  }
  public function store(Request $request)
  {
    $res1 = $this->repoist->add($request);
    return $res1;
  }
  public function index(Request $request)
  {
    $where = [];
    if (!empty($request->get('one_goods'))) {
      $where['like']['product_name'] = $request->get('one_goods');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (!empty($request->get('two_goods'))) {
      $where['like']['two_product_name'] = $request->get('two_goods');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    if (($request->has('status')) && $request->get('status') != '') {
      $where['where']['status'] = $request->get('status');
      // $where['where'] = ['sup_orderno' => $request->get('sup_orderno')];
    }
    // var_dump($where);
    if (!empty($request->get('pageNum'))) {
      $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
      $where['limit']['number'] = $request->get('pageSize');
    }

    if (empty($request->user->role_id) || $request->user->role_id != 1) {
      $where['where']['shop_id'] = $request->user->shop_id;
    }
    //时间验证 
    // var_dump($where);
    $data =  $this->repoist->index($where);
    return $data;
    $datas = [];
    if (!empty($data['data'])) {
      foreach ($data['data'] as $key => $v) {
        // $datas[$v['name']] = $v['value'];
      }
    }
    return $this->returnMsg(200, '查询成功', $datas, 2);
  }

  public function save(Request $request)
  {
    if (!$request->get('id')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = [
      'id' => $request->get('id'),
      'shop_id' => $request->user->shop_id
    ];
    return $this->repoist->edit($where, $request);
  }
  public function  show(Request $request, $id)
  {
    $where = [
      'id' => $id
    ];
    //查询一条数据
    $field = ['*'];
    $return = $this->repoist->getOne($where, $field);
    // if(!empty($return)){
    //   $return[''] = (int)$return['']
    // }
    // $return = json_decode(json_encode($return), true);
    if (!empty($return->goods_id)) {
      $return['one_goods'] = $return->goods_id;
    }
    if (!empty($return->two_goods_id)) {
      $return['two_goods'] = $return->two_goods_id;
    }
    if (!empty($return->order_status)) {
      $arr = explode(',', trim($return->order_status, ','));
      if (!empty($arr)) {
        foreach ($arr as $key => $v) {
          $arrs[$key] = $v * 10 / 10;
        }
      }
      $return['order_status'] = $arrs;
    }
    if (!empty($return->dist_black)) {
      $dist = explode(',', trim($return->dist_black, ','));
      if (!empty($dist)) {
        foreach ($dist as $key => $v) {
          // $v = (int)$v;
          $distS[$key] = $v * 10 / 10;
        }
        $return['dist_black'] = $distS;
      }
    }
    return $this->returnMsg(200, '查询成功', $return);
  }
  public function  showOne($id)
  {
    $where = ['id' => $id];
    //查询一条数据
    $return = $this->repoist->getOne($where, ['*']);
    return $this->returnMsg(200, '查询成功', $return);
  }

  public function listss(Request $request)
  {
    $where = [];
    if (!empty($request->get('name'))) {
      $where['where'] = ['name' => $request->get('name')];
    }

    $return = $this->repoist->lists($where);
    return $return;
  }

  public function destroy(Request $request, $id)
  {
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    return $this->repoist->del($where);
  }
  public function updateStatus(Request $request, $id)
  {
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    // 需要修改两个地方 一个是状态 一个 app是否上架
    $update = [
      'status' => $request->get('status'),
    ];

    return $this->repoist->edit($where, $update);
  }
}
